/*
* @Author: Marte
* @Date:   2017-07-13 16:56:38
* @Last Modified by:   Marte
* @Last Modified time: 2017-07-27 15:21:36
*/
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)
export default new Vuex.Store({
  state: {
    accountObj: localStorage.accountObj ? JSON.parse(localStorage.accountObj) : null,
    token: sessionStorage.getItem('token'),
    pageOpenedList: localStorage.pageOpenedList ? JSON.parse(localStorage.pageOpenedList) : [],
    tabIndex: '',
    tagsList: [],
    cachePage: localStorage.cachePage ? JSON.parse(localStorage.cachePage) : [],
    noCachePage: ['']
  },
  mutations: {
    SETTOKEN (state, token) {
      state.token = token
    },
    // 退出登录，清除登录相关的store数据
    loginOut (state) {
      state.accountObj = null;
      state.tabIndex = '';
      state.cachePage = [];
      state.pageOpenedList = [];
    },
    // tab索引设置
    SETTABINDEX (state, tabIndex) {
      state.tabIndex = tabIndex;
    },
    setTagsList (state, list) {
      state.tagsList.push(...list);
    },
    closePage (state, name) {
      state.cachePage.forEach((item, index) => {
        if (item === name) {
          state.cachePage.splice(index, 1);
        }
      });
      localStorage.cachePage = JSON.stringify(state.cachePage);
      this.commit('removeTag', name);
    },
    increateTag (state, tagObj) {
      if (!state.noCachePage.includes(tagObj.name)) {
        state.cachePage.push(tagObj.name);
        localStorage.cachePage = JSON.stringify(state.cachePage);
      }
      state.pageOpenedList.push(tagObj);
      localStorage.pageOpenedList = JSON.stringify(state.pageOpenedList);
    },
    removeTag (state, name) {
      state.pageOpenedList.map((item, index) => {
        if (item.name === name) {
          state.pageOpenedList.splice(index, 1);
        }
      });
      localStorage.pageOpenedList = JSON.stringify(state.pageOpenedList);
    },
    pageOpenedList (state, get) {
      let openedPage = state.pageOpenedList[get.index];
      if (get.argu) {
        openedPage.argu = get.argu;
      }
      if (get.query) {
        openedPage.query = get.query;
      }
      openedPage.title = get.title;
      state.pageOpenedList.splice(get.index, 1, openedPage);
      localStorage.pageOpenedList = JSON.stringify(state.pageOpenedList);
    },
    clearAllTags (state) {
      state.pageOpenedList = [];
      state.cachePage.length = 0;
      localStorage.pageOpenedList = JSON.stringify(state.pageOpenedList);
    },
    clearOtherTags (state, vm) {
      let currentName = vm.$route.name;
      let currentIndex = 0;
      state.pageOpenedList.forEach((item, index) => {
        if (item.name === currentName) {
          currentIndex = index;
        }
      });
      if (currentIndex === 0) {
        state.pageOpenedList.splice(1);
      } else {
        state.pageOpenedList.splice(currentIndex + 1);
        state.pageOpenedList.splice(0, currentIndex);
      }
      let newCachepage = state.cachePage.filter(item => {
        return item === currentName;
      });
      state.cachePage = newCachepage;
      localStorage.pageOpenedList = JSON.stringify(state.pageOpenedList);
    }
  },
  actions: {
    setToken ({commit}) {
      commit('SETTOKEN')
    }
  }
})

